/**
 * Copyright (c) 2008 Luciano Bustos - riaevolution.com, All Rights Reserved.
 * 
 * Contact luchyx@riaevolution.com
 * See www.riaevolution.com
 *  
 * Permission is hereby granted, free of charge, to any person obtaining a copy of 
 * this software and associated documentation files (the "Software"), to deal in 
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all 
 * copies or substantial portions of the Software.
 * 
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE. 
 * */
 
package com.riaevolution.components
{
	import flash.events.EventDispatcher;
	import flash.geom.Point;
	
	import mx.collections.ArrayCollection;
	import mx.collections.IViewCursor;
	import mx.core.IToolTip;
	import mx.core.UIComponent;
	import mx.events.ToolTipEvent;
	import mx.managers.ToolTipManager;
	
	public class ToolTipFreezer extends EventDispatcher
	{
		private static var _instance:ToolTipFreezer;
		
		private var collection:ArrayCollection;
		
		public function ToolTipFreezer()
		{
			collection = new  ArrayCollection();
		}
		
		public static function getInstance():ToolTipFreezer
		{
			if(!_instance) {
				_instance = new ToolTipFreezer();
			}
			return _instance;
		}
		
		public static function destroyAll (clearErrorString:Boolean = false):void {
	  		var cur:IViewCursor = _instance.collection.createCursor();
	  		var t:int = -1;
	  		while(!cur.afterLast) {
	  			if(clearErrorString) {
	  				UIComponent(cur.current.reference).errorString = '';
	  			} 
	  			ToolTipManager.destroyToolTip(cur.current.toolTip);
	  			cur.remove();
	  			cur.moveNext();
	  		}
	  	}
	  	
	  	public static function createErrorTip(on:UIComponent, preventDefault:Boolean = false):IToolTip {
	  		
	  		getInstance();	  			  		
	  		
	  		var lo:Point = on.parent.localToGlobal(new Point( on.x + on.width + 5, on.y));
	  		var tt:IToolTip;
	  		
	  		if(preventDefault) {
	  			on.addEventListener(ToolTipEvent.TOOL_TIP_CREATE,freeze);	
	  		}
	  		
	  		var id:int = _instance.getItemIndex(on);
	  		
	  		if( id != -1 )  {
	  			tt = _instance.collection.getItemAt(id).toolTip as IToolTip;
	  		} else {
	  			if(on.errorString != '') {
	  				tt = ToolTipManager.createToolTip(on.errorString, lo.x, lo.y, 'errorTipRight');
	  				_instance.collection.addItem({reference:on, toolTip:tt});
	  			}
	  		}
	  		//remove tooltip
	  		if(on.errorString == '') {
	  			if(tt) {
	  				ToolTipManager.destroyToolTip(tt);
	  				tt = null;
	  			}
	  			if(id != -1) {
	  				_instance.collection.removeItemAt(id);
	  				tt = null;
	  			}
	  		}
	  		if(tt) {
	  			tt.text = on.errorString;
	  			tt.move(lo.x,lo.y);
	  		}
	  		return tt;
	  	}
	  	public function getItemIndex(on:UIComponent):int {
	  		var cur:IViewCursor = _instance.collection.createCursor();
	  		var t:int = -1;
	  		while(!cur.afterLast) {
	  			if(cur.current.reference == on) {
	  				t = _instance.collection.getItemIndex(cur.current);
	  				break;
	  			}
	  			cur.moveNext();
	  		}
	  		return t;
	  	}
	  	
	  	public static function freeze(event:ToolTipEvent):void {
	  		event.toolTip = null;
	  		event.preventDefault();
	  		createErrorTip(event.currentTarget as UIComponent);
	  	}
	  	public function get dataProvider():ArrayCollection {
	  		return collection;
	  	}
	}
}